//
//  RegistSetupOne.m
//  HB
//
//  Created by 孙智慧 on 16/1/27.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "RegistSetupOne.h"
#import "RegistSetupTwo.h"
#import "LoginOperation.h"


@implementation RegistSetupOne

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    operation=[[NetWorkOperation alloc] initWithDelegate:self];

    
#pragma 设置头图片大小
    CGFloat  heigh =41;
    
    if(IsiPhone6 || IsiPhone6P)
    {
        heigh=41;
    }
    else{
        heigh=35;
    }
    
    NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:self.navImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:heigh];
    
    [self.view addConstraint:heightConstraint];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //显示导航栏
    [super.navigationController setNavigationBarHidden:NO animated:TRUE];
 
    [self setNavigationRight:@"下一步" sel:@selector(nextSteup)];
    
}

-(void) nextSteup
{
//    HUD = [[MBProgressHUD alloc] initWithView:self.view];
//    
//    [self.view addSubview:HUD];
//    
//    HUD.delegate = self;
//    
//    HUD.labelText = @"正在验证";
//    
//    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:operation animated:YES];
    
    [self waitingFor:@"正在验证" andMethod:@selector(myTask) onTarget:self];
    
}

-(void)myTask
{
    
    if(![Common isMobileNumber:self.txtMobile.text])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [Common showMsg:@"请输入正确的手机号！" andCancel:YES andHost:self];
        });
        
    }
    else
    {
        locked=YES;
        
        NSString *urlS= [FxGlobal getServiceUrl:@"user" andFuncName:@"IsExistsMobileRegister"];
        
        NSString *url= [NSString stringWithFormat:@"%@?mobile=%@",urlS,self.txtMobile.text];
        
        NSDictionary *dictInfo = @{@"url":url};
        
        [operation setOpInfo:dictInfo];
        
        [operation get:0];
        
        while (locked) {
            
        }
    }
    
}
-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    locked=NO;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"0"])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [BaseLoginController setMobile:self.txtMobile.text];
            
            //2.打开下一页
            RegistSetupTwo *registSetuoTwo= [[RegistSetupTwo alloc] init];
            
            registSetuoTwo.mobileStr=self.txtMobile.text;
            
            [self.navigationController  pushViewController:registSetuoTwo animated:YES];
        });
    }
    else
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [Common showMsg:@"手机号已经注册! 请更换手机号再继续。" andCancel:YES andHost:self];
        });
    }
}

//-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    return [self validateNumber:string];
//}
// 
//
//- (BOOL)validateNumber:(NSString*)number
//{
//    BOOL res = YES;
//    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
//    int i = 0;
//    while (i < number.length) {
//        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
//        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
//        if (range.length == 0) {
//            res = NO;
//            break;
//        }
//        i++;
//    }
//    return res;
//}

@end
